﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

using octalforty.Chinchillin.Actions.Sync;
using octalforty.Chinchillin.Shared.Deployment;
using octalforty.Chinchillin.Shared.Deployment.Metadata;
using octalforty.Chinchillin.Shared.Deployment.Resources;
using octalforty.Chinchillin.Shared.Deployment.Resources.Impl;

namespace octalforty.Chinchillin.Actions.Fs
{
    /// <summary>
    /// Represents an Action which deletes a file.
    /// </summary>
    [Action(Name)]
    public class DeleteFileAction : ActionBase
    {
        #region Private Constants
        private const string Name = "delete-file";
        private const string Location = "location";

        private static readonly ActionInfo actionInfo = new ActionInfo
            {
                Name = Name,
                Arguments = new[]
                    {
                        new ActionArgumentInfo(Location, ActionArgumentType.Uri)
                    }
            };
        #endregion

        #region Public Properties
        public IResourceProvider LocalResourceProvider
        { get; set; }

        public IFileSystemManager FileSystemManager
        { get; set; }
        #endregion

        public DeleteFileAction() :
            base(actionInfo)
        {
            
        }

        #region Action Members
        public override void Execute(IActionExecutionContext executionContext)
        {
            try
            {
                var location = (Uri)executionContext.Arguments[Location];

                TracingManager.TraceEvent(executionContext, TraceEventType.Information, 0, string.Format("Deleting file from '{0}'", location));

                FileSystemManager.DeleteFile(location.LocalPath);

                TracingManager.TraceEvent(executionContext, TraceEventType.Information, 0, string.Format("Deleted file from '{0}'", location));
            }
            catch(Exception e)
            {
                TracingManager.TraceEvent(executionContext, TraceEventType.Error, 0, e.ToString());
                throw;
            }
        }
        #endregion
    }
}
